package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: chouh
 * @DateTime: 2025/10/11 19:38
 * @Description:
 */
@Slf4j
@RestController
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/courses/subject/{subject}")
    public Result queryCoursesBySubject(@PathVariable String subject) {
        log.info("queryCoursesBySubject:{}",subject);
        List<Course> courses = courseService.queryCoursesBySubject(subject);
        return Result.success(courses);
    }

    @GetMapping("/courses/list")
    public Result queryAllCourses() {
        log.info("query all courses");
        List<Course> result = courseService.queryAllCourses();
        return Result.success(result);
    }

    @PutMapping("/courses")
    public Result updateCourse(@RequestBody Course course){
        log.info("update the course:{}",course);
        courseService.updateCourse(course);
        return Result.success();
    }

    @GetMapping("/courses/{id}")
    public Result queryCourseById(@PathVariable("id")Integer id){
        log.info("queryCourseById:{}", id);
        Course course = courseService.queryCourseById(id);
        return Result.success(course);
    }

    @PostMapping("/courses")
    public Result addCourse(@RequestBody Course course){
        log.info("addCourse:{}", course);
        courseService.addCourse(course);
        return Result.success();
    }

    @DeleteMapping("/courses/{id}")
    public Result deleteCourse(@PathVariable Integer id){
        log.info("deleteCourse:{}",id);
        courseService.deleteCourse(id);
        return Result.success();
    }
    @GetMapping("/courses")
    public Result listCourse(CourseDTO courseDTO){
        log.info("listCourse:{}", courseDTO);
        PageResult<Course> result = courseService.listCourse(courseDTO);
        return Result.success(result);
    }
}
